package com.totem.admin.controller;

import com.totem.admin.api.AdminProductServiceApi;
import com.totem.admin.service.IAdminProductService;
import com.totem.admin.vo.FileUrlVO;
import com.totem.base.constants.ErrorCode;
import com.totem.base.enums.ProductTypeEnum;
import com.totem.base.exception.BaseException;
import com.totem.base.model.PageVO;
import com.totem.base.model.Result;
import com.totem.base.mybatis.controller.BaseController;
import com.totem.product.model.TProductDetail;
import com.totem.product.model.TProductSpec;
import com.totem.product.model.TProductUrl;
import com.totem.product.model.TProducts;
import com.totem.product.service.ITProductsService;
import com.totem.product.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class AdminProductController extends BaseController<ITProductsService, TProducts> implements AdminProductServiceApi {
    @Autowired
    private IAdminProductService adminProductService;
    @Override
    public Result<Boolean> create(ProductCreateVO vo) {
        TProducts tProducts = TProducts.builder().build();
        tProducts.setProductName(vo.getProductName());
        ProductTypeEnum productTypeEnum = ProductTypeEnum.byCode(vo.getProductType());
        if(productTypeEnum!=null) {
            tProducts.setProductType(productTypeEnum.getCode());
        }
        tProducts.setIntroductory(vo.getIntroductory());

        TProductUrl tProductUrl = null;
        if(vo.getProductUrlVO()!=null) {
            tProductUrl = TProductUrl.builder().build();
            BeanUtils.copyProperties(vo.getProductUrlVO(), tProductUrl);
            String carouseUrls = vo.getProductUrlVO().getCarouselUrls().stream().collect(Collectors.joining(","));
            tProductUrl.setCarouselUrls(carouseUrls);
        }

        List<TProductSpec> specList = null;
        if(!CollectionUtils.isEmpty(vo.getProductSpecVOList())) {
            specList = new ArrayList<>();
            for(ProductSpecVO specVO: vo.getProductSpecVOList()) {
                TProductSpec tProductSpec = TProductSpec.builder().build();
                BeanUtils.copyProperties(specVO, tProductSpec);
                Integer usable = tProductSpec.getUsable();
                if(usable==null){
                    usable = 0;
                    tProductSpec.setUsable(usable);
                }
                Integer inventory = tProductSpec.getInventory();
                if(inventory==null||inventory==0){
                    inventory = usable;
                    tProductSpec.setInventory(inventory);
                }
                specList.add(tProductSpec);
            }
        }

        TProductDetail tProductDetail = null;
        if(vo.getProductDetailVO()!=null){
            tProductDetail = TProductDetail.builder().build();
            BeanUtils.copyProperties(vo.getProductDetailVO(), tProductDetail);
        }
        boolean result = service.createProduct(tProducts, tProductUrl, specList, tProductDetail);
        return Result.result("创建商品信息成功", "创建商品信息失败", result);
    }

    @Override
    public Result<Boolean> update(ProductUpdateVO vo) {
        Long pId = vo.getProductId();
        if(pId==null || pId<=0){
            throw new BaseException(ErrorCode.REQUIRED_PRODUCT_ID);
        }
        TProducts tProducts = TProducts.builder().build();
        tProducts.setProductName(vo.getProductName());
        ProductTypeEnum productTypeEnum = ProductTypeEnum.byCode(vo.getProductType());
        if(productTypeEnum!=null) {
            tProducts.setProductType(productTypeEnum.getCode());
        }
        tProducts.setIntroductory(vo.getIntroductory());
        tProducts.setId(pId);

        TProductUrl tProductUrl = null;
        if(vo.getProductUrlVO()!=null) {
            tProductUrl = TProductUrl.builder().build();
            BeanUtils.copyProperties(vo.getProductUrlVO(), tProductUrl);
            if(!CollectionUtils.isEmpty(vo.getProductUrlVO().getCarouselUrls())) {
                String carouseUrls = vo.getProductUrlVO().getCarouselUrls().stream().collect(Collectors.joining(","));
                tProductUrl.setCarouselUrls(carouseUrls);
            }
        }

        List<TProductSpec> specList = null;
        if(!CollectionUtils.isEmpty(vo.getProductSpecVOList())) {
            specList = new ArrayList<>();
            for(ProductSpecVO specVO: vo.getProductSpecVOList()) {
                TProductSpec tProductSpec = TProductSpec.builder().build();
                BeanUtils.copyProperties(specVO, tProductSpec);
                specList.add(tProductSpec);
            }
        }

        TProductDetail tProductDetail = null;
        if(vo.getProductDetailVO()!=null){
            tProductDetail = TProductDetail.builder().build();
            BeanUtils.copyProperties(vo.getProductDetailVO(), tProductDetail);
        }
        return Result.success(service.updateProduct(tProducts, tProductUrl, specList, tProductDetail));
    }

    @Override
    public Result<ProductVO> getInfoById(Long productId) {
        return Result.success(service.adminGetInfoById(productId));
    }

    @Override
    public Result<PageVO<ProductVO>> productPage(ProductPageVO vo) {
        return Result.success(service.adminProductPage(vo));
    }

    @Override
    public Result<Boolean> delete(ProductDeleteVO vo) {
        return Result.success(service.delete(vo));
    }

    @Override
    public Result<Boolean> shelf(ProductShelfVO vo) {
        return Result.success(service.shelf(vo));
    }

    @Override
    public Result<FileUrlVO> uploadFileCover(MultipartFile file) throws IOException {
        return Result.success(adminProductService.uploadFileCover(file));
    }

    @Override
    public Result<FileUrlVO> uploadFileCarousel(MultipartFile file) throws IOException {
        return Result.success(adminProductService.uploadFileCarousel(file));
    }

    @Override
    public Result<FileUrlVO> uploadFileVideo(MultipartFile file) throws IOException {
        return Result.success(adminProductService.uploadFileVideo(file));
    }
}
